/*
 * DatabaseUI.cpp
 *
 *  Created on: 17-Dec-2009
 *      Author: Atul Shukla (atul.shukla@iiitb.net)
 */
#include<iostream>
#include<stdio.h>
#include"Database.h"

void DatabaseUI::dUI(Database *db) {

	Status s;
	string filename;
	int n;//no of frames
	char ch;
	Page *p = new Page();
	int temp;
	do {

		cout << "\n1. Allocate Page";
		cout << "\n2. Deallocate Page";
		cout << "\n3. Read Page";
		cout << "\n4. Write Page";
		cout << "\n5. View Page";
		cout << "\n6. Free Pages List";
		cout << "\n7. Total No Of Pages Consumed";
		cout << "\n0. Exit";
		cout << "\nInput your choice:: ";
		cin >> ch;

		switch (ch) {

		case '1':
			if (db != 0) {
				db->allocatePage(temp);
				cout << "\n Page Allocated::" << temp;
			} else
				cout << "Database Not Opened";
			break;
		case '2':
			if (db != 0) {
				cout << "\nInput Page No::";
				cin >> temp;
				s = db->deallocatePage(temp);
				cout << s;
			} else
				cout << "Database Not Opened";
			break;
		case '3':
			if (db != 0) {
				cout << "\nInput Page No::";
				cin >> temp;
				db->readPage(temp, p);
			} else
				cout << "Database Not Opened";
			break;
		case '4':
			if (db != 0) {
				Page* pg = new Page();
				string data = "";
				cout << "\nEnter String to write";
				getline(cin, data);
				cout << "\nEnter Page No";
				cin >> temp;
				pg->pageData = (unsigned char*) data.c_str();
				pg->pageno = temp;
				db->writePage(temp, pg);
				cout << "\nPage Written";
			} else
				cout << "Database Not Opened";
			break;
		case '5':
			db->view(p);
			break;
		case '6':
			if (db != 0) {
				cout << db->getFreeList();
			} else
				cout << "Database Not Opened";
			break;
		case '7':
			if (db != 0) {
				cout << "Total No Of Pages" << db->getNextEmptyPage();
			} else
				cout << "Database Not Opened";

			break;
		default:
			break;
		}
	} while (ch != '0');//infinite loop
}

